//
//  NHRequestManager.m
//  LZNeiHan
//
//  Created by 梁梓烽 on 16/10/4.
//  Copyright © 2016年 liangzifeng. All rights reserved.
//

#import "NHRequestManager.h"
#import <AFHTTPSessionManager.h>

@interface AFHTTPSessionManager (share)

+(instancetype)shareManager;
@end

@implementation AFHTTPSessionManager (share)

+(instancetype)shareManager
{
    static AFHTTPSessionManager *_manager ;
    dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _manager = [AFHTTPSessionManager manager];
        _manager.responseSerializer.acceptableContentTypes  =  [NSSet setWithObjects:@"application/json", @"text/json",@"text/plain", @"text/javascript",@"text/html", nil];
    });    return _manager;

}

@end


@implementation NHRequestManager
+(void)Get:(NSString*)url params:(NSDictionary *)params responseSerializerType:(NHRsponseSerializerType)type success:(void (^)(id  response ))success
   failure:(void(^)(NSError * error))failure;
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager shareManager];
    //设置请求头
    [manager.requestSerializer setValue:[self cookie ] forHTTPHeaderField:@"Cookie"];
    
    if (type!=NHResponseSeializerTypeJSON && type!= NHResponseSeializerTypeDefault) {
        manager.responseSerializer  = [self getType:type];
    }
    
    [manager GET:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if (success) {
            success(responseObject);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
}

    

+(void)Post:(NSString*)url params:(NSDictionary *)params responseSerializerType:(NHRsponseSerializerType)type success:(void (^)(id  response))success
    failure:(void(^)(NSError * error))failure
{
    
     AFHTTPSessionManager *manager = [AFHTTPSessionManager shareManager];
    
    if (type != NHResponseSeializerTypeJSON && type != NHResponseSeializerTypeDefault) {
        manager.responseSerializer  = [self getType:type];
    }
    
    AFSecurityPolicy *policy  =  [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
    policy.allowInvalidCertificates = YES;
    manager.securityPolicy =policy ;
    
    [manager POST:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
}



+ (void)POST:(NSString *)URLString
  parameters:(id)parameters
responseSeializerType:(NHRsponseSerializerType)type
constructingBodyWithBlock:(void (^)(id<AFMultipartFormData> formData))block
     success:(void (^)(id responseObject))success
     failure:(void (^)(NSError *error))failure
{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager shareManager];
    
    if (type != NHResponseSeializerTypeJSON && type != NHResponseSeializerTypeDefault) {
        manager.responseSerializer  = [self getType:type];
    }
    
    [manager POST:URLString parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        if (block) {
            block(formData);
        }
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];

}



+(void)cancelAllRequests
{
    AFHTTPSessionManager *manager =[AFHTTPSessionManager shareManager];
    [manager.operationQueue cancelAllOperations];
}

#pragma mark -其他

+(AFHTTPResponseSerializer *)getType:(NHRsponseSerializerType)type
{
    switch (type) {
        case NHResponseSeializerTypeDefault:
        {
            return [AFJSONResponseSerializer serializer];
        }
            break;
            
        case NHResponseSeializerTypeJSON:
        {
            return [AFJSONResponseSerializer serializer];
        }
            break;
        case NHResponseSeializerTypeXML:
        {
            return [AFXMLParserResponseSerializer serializer];
        }
            break;
        case NHResponseSeializerTypePlist:
        {
            return [AFPropertyListResponseSerializer serializer];
        }
            break;
            
        case NHResponseSeializerTypeCompound:
        {
            return [AFCompoundResponseSerializer serializer];
        }
            break;
        case NHResponseSeializerTypeImage:
        {
            return [AFImageResponseSerializer serializer];
        }
            break;
            
        case NHResponseSeializerTypeData:
        {
            return [AFHTTPResponseSerializer serializer];
        }
            break;
        default:
        {
            return  [AFJSONResponseSerializer serializer];
        }
            break;
    }
}


+(NSString *)cookie
{
      return @"_ga=GA1.2.732729183.1467731127; install_id=5316804410; login_flag=319157cead347271ef233ba429923e3b; qh[360]=1; sessionid=b391787a2cd16be0f914259f0cf829a4; sid_guard=\"b391787a2cd16be0f914259f0cf829a4|1473218826|2591916|Fri\054 07-Oct-2016 03:25:42 GMT\"; sid_tt=b391787a2cd16be0f914259f0cf829a4; uuid=\"w:9ce15113cb34468795d7aff3edddd670";
}

@end
